/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import java.util.Calendar;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class Unity extends AbstractEntity {

    @Column(nullable = false, length = 60)
    @Size(min = 3, message = "Mínimo três letras")
    private String name;
    @Column(nullable = false)
    private short capacity;
    @Temporal(TemporalType.DATE)
    private Calendar activated;
    @Temporal(TemporalType.DATE)
    private Calendar deactivated;
    @ManyToOne
    @JoinColumn(name = "organization_id", nullable = false)
    private Organization organization;
    @ManyToOne
    @JoinColumn(name = "father_id")
    private Unity unityFather;
    @ManyToOne
    @JoinColumn(name = "period_id")
    private Period period;
    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;
    @OneToMany(mappedBy = "unity", fetch = FetchType.LAZY, orphanRemoval = true)
    private List<Actor> actors;

    public Unity() {
    }

    @Override
    public void generateHash() {
        if (getId() == null) {
            String unityHashString = "" + this.getName()
                    + ((this.getOrganization() != null) ? this.getOrganization().getId() : null)
                    + ((this.getUnityFather() != null) ? this.getUnityFather().getId() : null)
                    + ((this.getActivated() != null) ? CalendarUtil.getFormatedDate(this.getActivated()) : null)
                    + ((this.getDeactivated() != null) ? CalendarUtil.getFormatedDate(this.getDeactivated()) : null);
            this.setId(Long.valueOf(EncryptionUtil.encrypt(unityHashString).hashCode()));
        }
    }

    public Unity(String name, short capacity, Calendar activated, Calendar deactivated, Organization organization, Unity unityFather, Period period, Person person) {
        this.name = name;
        this.capacity = capacity;
        this.activated = activated;
        this.deactivated = deactivated;
        this.organization = organization;
        this.unityFather = unityFather;
        this.period = period;
        this.person = person;
    }

    //Activation year equals of deactivation year
    public boolean isSameYearActvation() {
        return this.getActivated() != null
                && this.getDeactivated() != null
                && this.getActivated().get(Calendar.YEAR) == this.getDeactivated().get(Calendar.YEAR);
    }

    public short getCapacity() {
        return capacity;
    }

    public void setCapacity(short capacity) {
        this.capacity = capacity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Calendar getActivated() {
        return activated;
    }

    public void setActivated(Calendar activated) {
        this.activated = activated;
    }

    public Calendar getDeactivated() {
        return deactivated;
    }

    public void setDeactivated(Calendar deactivated) {
        this.deactivated = deactivated;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Period getPeriod() {
        return period;
    }

    public void setPeriod(Period period) {
        this.period = period;
    }

    public Unity getUnityFather() {
        return unityFather;
    }

    public void setUnityFather(Unity unityFather) {
        this.unityFather = unityFather;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Actor> getActors() {
        return actors;
    }

    public void setActors(List<Actor> actors) {
        this.actors = actors;
    }
}
